package com.ruoyi.system.service.honor.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.honor.person.PersonalHonor;
import com.ruoyi.system.mapper.honor.PersonalHonorMapper;
import com.ruoyi.system.service.honor.IPersonalHonorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 个人荣誉Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-29
 */
@Service
public class PersonalHonorServiceImpl implements IPersonalHonorService
{
    @Autowired
    private PersonalHonorMapper personalHonorMapper;

    /**
     * 查询个人荣誉
     * 
     * @param id 个人荣誉主键
     * @return 个人荣誉
     */
    @Override
    public PersonalHonor selectPersonalHonorById(Long id)
    {
        return personalHonorMapper.selectPersonalHonorById(id);
    }

    /**
     * 查询个人荣誉列表
     * 
     * @param personalHonor 个人荣誉
     * @return 个人荣誉
     */
    @Override
    public List<PersonalHonor> selectPersonalHonorList(PersonalHonor personalHonor)
    {
        //如果是老师，则只显示自己上传的
        if(personalHonor.getCurrentRole()!=null&&personalHonor.getCurrentRole().equals("teacher")){
            personalHonor.setWinnerId(getUserId());
        }
        //如果是部门，则显示老师是系里的所有
        if(personalHonor.getCurrentRole()!=null&&personalHonor.getCurrentRole().equals("department")){
            personalHonor.setDeptId(SecurityUtils.getDeptId());
        }
        return personalHonorMapper.selectPersonalHonorList(personalHonor);
    }

    /**
     * 新增个人荣誉
     * 
     * @param personalHonor 个人荣誉
     * @return 结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(
                    value = "teacherStatistics_getIndexInfo",
                    key = "#personalHonor.winnerId + '-' + T(java.time.format.DateTimeFormatter).ofPattern('yyyy').format(T(java.time.Instant).ofEpochMilli(#personalHonor.issueTime.time).atZone(T(java.time.ZoneId).systemDefault()).toLocalDate())"
            ),
            @CacheEvict(
                    value = "departmentStatistics_getIndexInfo",
                    key = "T(com.ruoyi.common.utils.SecurityUtils).getDeptId() + '-' + T(java.time.format.DateTimeFormatter).ofPattern('yyyy').format(T(java.time.Instant).ofEpochMilli(#personalHonor.issueTime.time).atZone(T(java.time.ZoneId).systemDefault()).toLocalDate())"
            )
    })
    public int insertPersonalHonor(PersonalHonor personalHonor)
    {
        return personalHonorMapper.insertPersonalHonor(personalHonor);
    }

    /**
     * 修改个人荣誉
     * 
     * @param personalHonor 个人荣誉
     * @return 结果
     */
    @Override
    public int updatePersonalHonor(PersonalHonor personalHonor)
    {
        return personalHonorMapper.updatePersonalHonor(personalHonor);
    }

    /**
     * 批量删除个人荣誉
     * 
     * @param ids 需要删除的个人荣誉主键
     * @return 结果
     */
    @Override
    public int deletePersonalHonorByIds(Long[] ids)
    {
        return personalHonorMapper.deletePersonalHonorByIds(ids);
    }

    /**
     * 删除个人荣誉信息
     * 
     * @param id 个人荣誉主键
     * @return 结果
     */
    @Override
    public int deletePersonalHonorById(Long id)
    {
        return personalHonorMapper.deletePersonalHonorById(id);
    }
}
